package huoye.shops.huoye.mine.activity;

import android.graphics.Bitmap;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import huoye.shops.R;
import huoye.shops.base.BaseActivity;
import huoye.shops.huoye.mine.model.AccountList;
import huoye.shops.huoye.mine.model.AddAccountInfo;
import huoye.shops.huoye.mine.model.AuthStateData;
import huoye.shops.huoye.mine.presenter.AddAccoundCodePresenter;
import huoye.shops.huoye.mine.view.IAddAccoundCodeView;

public class AddAccoundCodeActivity extends BaseActivity implements IAddAccoundCodeView{


    @BindView(R.id.add_accound_iv_binding_wx)
    ImageView ivBindingWX;
    private AddAccoundCodePresenter mAddAccoundCodePresenter;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_add_accound_code;
    }

    @Override
    protected void initView(@Nullable Bundle bundle) {
        setTitle("新增账户");
        mAddAccoundCodePresenter = new AddAccoundCodePresenter();
        mAddAccoundCodePresenter.addTaskListener(this);
        mAddAccoundCodePresenter.onStart();
        mAddAccoundCodePresenter.addAccountInfo();

    }

    private AddAccountInfo mAddAccountInfo;
    @Override
    public void getCodeStr(AddAccountInfo addAccountInfo) {
        this.mAddAccountInfo = addAccountInfo;
        Bitmap mBitmap = CodeUtils.createImage(addAccountInfo.getQrCodeUrl(), 400, 400, null);
        ivBindingWX.setImageBitmap(mBitmap);
        timer();
    }

    @Override
    public void getCodeReturn(AuthStateData authStateData) {
        mAddAccoundCodePresenter = null;
        AccountList accountList = new AccountList();
        accountList.setWxName(authStateData.getNickName());
        accountList.setWxHeadImg(authStateData.getHeadImg());
        accountList.setMobile(authStateData.getMobile());
        accountList.setUserId(authStateData.getAuthUserId());
        accountList.setEidt(false);
        startActivity(EditAccoundActivity.class,accountList);
        finish();
    }



    private Handler handler = new Handler();
    private Runnable mRunnable;

    private void timer() {
        mRunnable = new Runnable() {
            @Override
            public void run() {
                if(mAddAccoundCodePresenter != null){
                    //获取数据 间隔几秒做一个请求
                    mAddAccoundCodePresenter.getAuthState(mAddAccountInfo.getTimeStamp());
                    Log.e("timetest","1");
                }
                handler.postDelayed(this, 5000);
            }
        };
        handler.postDelayed(mRunnable, 5000);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
      //  handler.removeCallbacks(mRunnable);
        handler.removeCallbacksAndMessages(null);
    }


}
